<?php
//此控制器用于测试
class TestAction extends CommAction{
   

   public function get_wx_location(){
        $this->display();
   }

   public function get_jw(){
        $data['latitude'] = $_POST['latitude'];
        $data['longitude'] = $_POST['longitude'];
        //$data['latitude'] = '31.981813451873';
        //$data['longitude'] = "118.77716089869";
        $add = $this->CoordinatetransferAddress($data['longitude'],$data['latitude']);
        //var_dump($add);
        
        
        $this->ajaxReturn(array('msg'=>$add));
   }

  public function CoordinatetransferAddress($longitude,$latitude){ 
    

   $url ="http://api.map.baidu.com/geoconv/v1/?coords=".$longitude.",".$latitude."&from=3&to=5&ak=BEL4PrsQBKNGCz2rUMOwPVhSDBcTDGhQ";
   $content = $this->http($url, NULL, 'POST', array("Content-type: text/html; charset=utf-8")); 
   $obj = json_decode($content);
   $obj =$obj->result; 
   $obj = $obj['0']; 
   $longitude=$obj->x; 
   $latitude=$obj->y;
   //$res['longitude'] = $longitude;
   //$res['latitude'] = $latitude;
   //return $res;

   $url ="http://api.map.baidu.com/geocoder/v2/?ak=BEL4PrsQBKNGCz2rUMOwPVhSDBcTDGhQ&callback=renderReverse&location=".$latitude.",".$longitude."&output=json&pois=1";
   $content = $this->http($url, NULL, 'POST', array("Content-type: text/html; charset=utf-8")); 
   $content = substr($content,29);
   $content = substr($content,0,-1);
   $obj = json_decode($content);
   $obj =$obj->result; ;
   return $obj->formatted_address; 
   }


   function http($url, $params, $method = 'GET', $header = array(), $multi = false){
   $opts = array(
   CURLOPT_TIMEOUT => 30,
   CURLOPT_RETURNTRANSFER => 1,
   CURLOPT_SSL_VERIFYPEER => false,
   CURLOPT_SSL_VERIFYHOST => false,
   CURLOPT_HTTPHEADER => $header
   );
   /* 根据请求类型设置特定参数 */
   switch(strtoupper($method)){
   case 'GET':
   $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
   break;
   case 'POST':
   //判断是否传输文件
   $params = $multi ? $params : http_build_query($params);
   $opts[CURLOPT_URL] = $url;
   $opts[CURLOPT_POST] = 1;
   $opts[CURLOPT_POSTFIELDS] = $params;
   break;
   default:
   throw new Exception('不支持的请求方式！');
   }
   /* 初始化并执行curl请求 */
   $ch = curl_init();
   curl_setopt_array($ch, $opts);
   $data = curl_exec($ch);
   $error = curl_error($ch);
   curl_close($ch);
   if($error) throw new Exception('请求发生错误：' . $error);
   return $data;


   }




}
